package com.example.uiview_025;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ProgressBarActivity extends Activity {

	private ProgressBar bar = null;

	private Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_progress_bar);
		bar = (ProgressBar) findViewById(R.id.progressBar1);

		new Thread(new Runnable() {
			boolean flag = true;

			@Override
			public void run() {
				while (flag) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					bar.incrementProgressBy(10);

					handler.post(new Runnable() {

						@Override
						public void run() {
							if (bar.getProgress() >= bar.getMax()) {
								ViewGroup viewGroup = (ViewGroup) bar
										.getParent();
								if (viewGroup != null) {
									viewGroup.removeView(bar);
									TextView textView = new TextView(
											ProgressBarActivity.this);
									textView.setText("done");
									viewGroup.addView(textView);
								}
								flag = false;
							}
						}
					});
				}
			}
		}).start();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.progress_bar, menu);
		return true;
	}

	public void add(View viwe) {
		bar.incrementProgressBy(10);
	}

	public void reduce(View view) {
		bar.incrementProgressBy(-10);
	}

}
